package cn.iocoder.yudao.module.wine.controller.app.complaininfo;

import cn.iocoder.yudao.framework.ip.core.utils.AreaUtils;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import java.util.*;
import java.util.stream.Collectors;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.wine.controller.app.complaininfo.vo.*;
import cn.iocoder.yudao.module.wine.service.complaininfo.ComplainInfoService;

@Tag(name = "用户 APP - 投诉信息")
@RestController
@RequestMapping("/wine/complain-info")
@Validated
public class AppComplainInfoController {

    @Resource
    private ComplainInfoService complainInfoService;

    @PostMapping("/create")
    @Operation(summary = "发布投诉信息")
    public CommonResult<Integer> createComplainInfo(@Valid @RequestBody AppComplainInfoSaveReqVO createReqVO) {
        return success(complainInfoService.appCreateComplainInfo(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新投诉信息")
    public CommonResult<Boolean> updateComplainInfo(@Valid @RequestBody AppComplainInfoSaveReqVO updateReqVO) {
        complainInfoService.appUpdateComplainInfo(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "下架投诉信息")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> deleteComplainInfo(@RequestParam("id") Long id) {
        complainInfoService.appDeleteComplainInfo(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得投诉信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<AppComplainInfoRespVO> getComplainInfo(@RequestParam("id") Long id) {
        AppComplainInfoRespVO complainInfo = complainInfoService.appGetComplainInfo(id);
        if (complainInfo != null) {
            complainInfo.setUserAreaName(AreaUtils.format(complainInfo.getUserAreaId(), " "));
        }
        return success(complainInfo);
    }

    @GetMapping("/list")
    @Operation(summary = "获得投诉信息列表")
    public CommonResult<PageResult<AppComplainInfoRespVO>> getComplainInfoList(@Valid AppComplainInfoPageReqVO pageReqVO) {
        PageResult<AppComplainInfoRespVO> page = complainInfoService.getComplainInfoList(pageReqVO);
        List<AppComplainInfoRespVO> list = page.getList();
        List<AppComplainInfoRespVO> collect = list.stream().peek(item -> {
            item.setAreaName(AreaUtils.format(item.getAreaId(), " "));
            item.setUserAreaName(AreaUtils.format(item.getUserAreaId(), " "));
        }).collect(Collectors.toList());
        page.setList(collect);
        return success(page);
    }

    @GetMapping("/my-publish-list")
    @Operation(summary = "获得我发布的投诉信息列表")
    public CommonResult<List<AppComplainInfoRespVO>> getMyPublishList(@Valid AppComplainInfoPageReqVO pageReqVO) {
        List<AppComplainInfoRespVO> list = complainInfoService.getMyPublishList(pageReqVO);
        List<AppComplainInfoRespVO> collect = list.stream().peek(item -> {
            item.setAreaName(AreaUtils.format(item.getAreaId(), " "));
            item.setUserAreaName(AreaUtils.format(item.getUserAreaId(), " "));
        }).collect(Collectors.toList());
        return success(collect);
    }

}
